﻿using System;
using System.Collections.Generic;
using System.Text;

namespace charpter_2_2
{
    public class ProcessPipeline : System.ComponentModel.Component
    {
        #region 作为事件键值的对象
        private static readonly object startEvent = new object();
        private static readonly object preProcessEvent = new object();
        private static readonly object postProcessEvent = new object();
        private static readonly object endEvent = new object();
        #endregion

        #region 事件的定义
    public event EventHandler StartProcess
    {
        add { this.Events.AddHandler(startEvent, value); }
        remove { this.Events.RemoveHandler(startEvent, value); }
    }

    public event EventHandler PreProcess
    {
        add { this.Events.AddHandler(preProcessEvent, value); }
        remove { this.Events.RemoveHandler(preProcessEvent, value); }
    }

    public event EventHandler PostProcess
    {
        add { this.Events.AddHandler(postProcessEvent, value); }
        remove { this.Events.RemoveHandler(postProcessEvent, value); }
    }

    public event EventHandler EndProcess
    {
        add { this.Events.AddHandler(endEvent, value); }
        remove { this.Events.RemoveHandler(endEvent, value); }
    }
        #endregion

        #region 触发事件的方法
        protected void OnStartProcess(EventArgs e)
        {
            if (this.Events[startEvent] != null)
            {
                (this.Events[startEvent] as EventHandler)(this, e);
            }
        }

        protected void OnPreProcess(EventArgs e)
        {
            if (this.Events[preProcessEvent] != null)
            {
                (this.Events[preProcessEvent] as EventHandler)(this, e);
            }
        }

        protected void OnPostProcess(EventArgs e)
        {
            if (this.Events[postProcessEvent] != null)
            {
                (this.Events[postProcessEvent] as EventHandler)(this, e);
            }
        }

        protected void OnEndProcess(EventArgs e)
        {
            if (this.Events[endEvent] != null)
            {
                (this.Events[endEvent] as EventHandler)(this, e);
            }
        }
        #endregion

        // 触发处理管线的方法
        public void Process()
        {
            Console.WriteLine("开始处理 ");
            this.OnStartProcess(EventArgs.Empty);
            Console.WriteLine("准备处理");
            this.OnPreProcess(EventArgs.Empty);
            Console.WriteLine("正在处理中");
            Console.WriteLine("处理完成");
            this.OnPostProcess(EventArgs.Empty);
            Console.WriteLine("结束处理");
            this.OnEndProcess(EventArgs.Empty);
        }

    }

class Program
{
    static void Main(string[] args)
    {
        ProcessPipeline process = new ProcessPipeline();
        process.StartProcess += new EventHandler(process_StartProcess);
        process.PreProcess += new EventHandler(process_PreProcess);
        process.PostProcess += new EventHandler(process_PostProcess);
        process.EndProcess += new EventHandler(process_EndProcess);

        process.Process();
    }

    static void process_StartProcess(object sender, EventArgs e)
    {
        Console.WriteLine("开始处理的事件处理...");
    }

    static void process_PreProcess(object sender, EventArgs e)
    {
        Console.WriteLine("处理前的事件处理...");
    }

    static void process_PostProcess(object sender, EventArgs e)
    {
        Console.WriteLine("处理后的事件处理...");
    }

    static void process_EndProcess(object sender, EventArgs e)
    {
        Console.WriteLine("处理完成的事件处理...");
    }






}
}
